package com.example.springbootbabyu.controller;


import com.example.springbootbabyu.utils.JwtTokenUtil;
import com.example.springbootbabyu.utils.ResultUtil;
import com.example.springbootbabyu.entity.Sku;
import com.example.springbootbabyu.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 功能
 * 作者：梦天
 * 日期：2025/4/17 21:04
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin
@Slf4j
public class GoodsController {
    @Autowired
    private  GoodsService goodsService;

    /**
     * 获取商品详情
     * @param id 商品id
     * @return 商品详情
     */
    @GetMapping("/detail")
    public ResultUtil getGoodsDetail(@RequestParam("id") String id) {
        try {
            log.info("获取商品详情, 商品ID: {}", id);
            return ResultUtil.success(1, "操作成功", goodsService.getGoodsDetailCache(id));
        } catch (Exception e) {
            log.error("获取商品详情失败, 商品ID: {}, 异常信息: {}", id, e.getMessage());
            return ResultUtil.error(0, "操作失败");
        }
    }
    @GetMapping("/stock/{id}")
    public ResultUtil<Sku> getStockById(@PathVariable("id") String skuId){
        try {
            log.info("获取商品库存, 商品ID: {}", skuId);
            return ResultUtil.success(1, "操作成功", goodsService.getStockById(skuId));
        } catch (Exception e) {
            log.error("获取商品库存失败, 商品ID: {}, 异常信息: {}", skuId, e.getMessage());
            return ResultUtil.error(0, "操作失败");
        }
    }
    /**
     * 获取热销商品
     * @param id    商品ID
     * @param type  热销类型（1: 24小时热销榜 2: 周热销榜）
     * @param limit 查询数量（默认3）
     * @return 热销商品列表
     */
    @GetMapping("/hot")
    public ResultUtil getHotGoods(
            @RequestParam String id,
            @RequestParam String type,
            @RequestParam(defaultValue = "3") Integer limit) {
        try {
            log.info("获取热销商品参数 id: {}, type: {}, limit: {}", id, type, limit);
            return ResultUtil.success(1, "操作成功", goodsService.getHotGoods(id, type, limit));
        } catch (Exception e) {
            log.error("获取热销商品失败, 商品ID: {}, 异常信息: {}", id, e.getMessage());
            return ResultUtil.error(0, "操作失败");
        }
    }
    @GetMapping("/relevant")
    public ResultUtil getRelevantGoods(
            @RequestParam(defaultValue = "3") Integer limit) {
        try {
            log.info("获取相关商品参数 limit: {}", limit);
            return ResultUtil.success(1, "操作成功", goodsService.getRelevantGoods(limit));
        } catch (Exception e) {
            log.error("获取相关商品失败, 异常信息: {}", e.getMessage());
            return ResultUtil.error(0, "操作失败");
        }
    }
}
